/*
 * Copyright (c) 2023, 2023,yzc.cn All rights reserved.
 *
 */
package com.crmapi.controller;

import com.crmapi.group.Save;
import com.crmapi.pojo.Contract;
import com.crmapi.pojo.PageBean;
import com.crmapi.pojo.Result;
import com.crmapi.service.ContractService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * <p>Project: CRMSystem - ContractController</p>
 * <p>Powered by yzc On 2023-11-28 14:25:11</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
@Api(tags = "合同管理")
@RestController
@Slf4j
@RequestMapping("/api_v1.0")
public class ContractController extends BaseController{

    @Autowired
    ContractService contractService;
    //保存合同信息
    @ApiOperation("保存合同，批量保存附件，批量保存回款计划")
    @PostMapping("/contract/save")
    public Result save(@Validated(value = {Save.class}) @RequestBody Contract contract , BindingResult result){
        validator(result);
        int rs = contractService.save(contract);
        if (rs !=1 )
            return Result.error("保存合同失败");
        return Result.success("保存合同成功");
    }

    //条件分页查询
    @ApiOperation("条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页显示的记录数",required = true),
            @ApiImplicitParam(name = "code",value = "合同编码"),
            @ApiImplicitParam(name = "name",value = "合同名称"),
            @ApiImplicitParam(name = "customerName",value = "客户名称"),
            @ApiImplicitParam(name = "status",value = "状态"),
            @ApiImplicitParam(name = "contractTime",value = "合同签订时间")
    })
    @PostMapping("/contract/list")
    public Result queryContract(@RequestParam(defaultValue = "1") Integer page ,
                                @RequestParam(defaultValue = "5")Integer pageSize,
                                String code, String name, String customerName, Integer status, String contractTime,
                                Integer type,String userCode){
        log.info("分页参数：{}，{}，条件参数:{}，{}，{}，{}，{}",page,pageSize,code,name,customerName,status,contractTime,type,userCode);
        PageBean pageBean = contractService.queryContract(page, pageSize, code, name, customerName, status, contractTime,type,userCode);
        return Result.success(pageBean);

    }

    //批量删除
    @ApiIgnore("批量删除")
    @ApiImplicitParam(name = "ids",value = "要删除的id",required = true)
    @DeleteMapping("/contract/del/{ids}")
    public Result del(@PathVariable List<String> ids){
      int rs = contractService.del(ids);
      if(rs > 0)
          return  Result.success("删除成功");
      return Result.error("删除失败");
    }
}
